package cc.wfu.trigger.listener;

import cc.wfu.domain.order.service.IProductOrder;
import cc.wfu.types.event.BaseEvent;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc:
 * @Date: 2024-04-28-13:58
 */
@Slf4j
@Component
public class OrderCustomer {
    @Value("${spring.rabbitmq.topic.compensation_order}")
    private String topic;
    @Resource
    private IProductOrder orderService;

    @RabbitListener(queuesToDeclare = @Queue(value = "compensation_order"))
    public void listener(String message) {
        try {
            log.info("监听订单发货消息 topic: {} message: {}", topic, message);
            // 转换对象
            BaseEvent.EventMessage<String> eventMessage =
                    JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<String>>() {}.getType());
            String orderId = eventMessage.getData();
            orderService.deliverGoods(orderId);
        } catch (Exception e) {
            log.error("监听订单发货消息，消费失败 topic: {} message: {}", topic, message);
            throw e;
        }
    }
}
